/**
 * datagram packet protocol:
 * crc          : 32bit crc32 value of all fields excluding itself,
 *                including 'identifier', 'length', 'type', 'reversed', 'body'
 * identifier   : 32bit unsigned int,
 *                the request is same as the response
 * length       : 32bit unsigned int,
 *                just means the size of 'body' field
 * type         : 8bit char,
 *                request type is [0,128],  [0,0x0F] is for system need,
 *                response type is (128,256],
 *                [response type] = [response type] | (0x01 << 7)
 *                defined types as following:
 *                0x01  - heartbeat request
 *                0x10  - business request
 * reversed     : 24bit char[3], padding with '\0'
 * body         : bytes' count indicated by the 'length' field,
 *                for the better performance,
 *                if this is request, max value is 8192 (8k),
 *                if this is response, max value is 81920 (80k)
 *
 *
 *
 */
package com.dd.keel.core.framework.protocol;

import java.util.Arrays;

import com.dd.keel.core.framework.CPacket;

/**
 * SOA message
 * @author Kevin.XU
 *
 */
public class SoaMsg implements CPacket{

	final static public int MESSAGE_HEADER_LENGTH = 16;
	
	private long crc        = 0;  /*32-bit*/
	private long identifier = 0;  /*32-bit*/
	private long length     = 0;  /*32-bit*/
	private byte type       = 0;  /*8-bit*/
	private byte[] reversed    ;  /*24-bit*/
	private byte[] body        ;  /*(length)*/

	
	public SoaMsg(){}
	

	public SoaMsg(long crc, long identifier, long length, byte type,
			byte[] reversed, byte[] body) {
		this.crc = crc;
		this.identifier = identifier;
		this.length = length;
		this.type = type;
		this.reversed = reversed;
		this.body = body;
	}


	public SoaMsg(long crc, long identifier, long length, byte type,
			byte[] body) {
		this.crc = crc;
		this.identifier = identifier;
		this.length = length;
		this.type = type;
		this.body = body;
	}	
	
	public long getCrc() {
		return crc;
	}


	public void setCrc(long crc) {
		this.crc = crc;
	}


	public long getIdentifier() {
		return identifier;
	}


	public void setIdentifier(long identifier) {
		this.identifier = identifier;
	}


	public long getLength() {
		return length;
	}


	public void setLength(long length) {
		this.length = length;
	}


	public byte getType() {
		return type;
	}


	public void setType(byte type) {
		this.type = type;
	}


	public byte[] getReversed() {
		return reversed;
	}


	public void setReversed(byte[] reversed) {
		this.reversed = reversed;
	}


	public byte[] getBody() {
		return body;
	}


	public void setBody(byte[] body) {
		this.body = body;
	}

	public static byte getResponseType(byte type){
		return (byte) (type | (byte)(1 << 7));
	}

	@Override
	public String toString() {
		return "SoaMsg [crc=" + crc + ", identifier=" + identifier
				+ ", length=" + length + ", type=" + type 
				+ "]";
	}


	@Override
	public String packetSequence() 
	{
		return identifier+"";
	}
	
}